'''
Created on 5 mai 2010

@author: philip
'''

from django.db import models
from djangotoolbox.fields import ListField
from django.core.exceptions import ObjectDoesNotExist

# Bar Cafe historic nature parc restaurant sport

class PlaceCategories:
    supported = ["bar", "cafe", "historic", "nature", "parc", "restaurant", "sport"]

class Place(models.Model):
    community_id = models.CharField(max_length=50)
    name = models.CharField(max_length=100)
    lat = models.FloatField()
    lon = models.FloatField()
    categories = ListField(models.CharField(max_length=100)) 
    osmId = models.CharField(max_length=50)
    
    def getFirstCategory(self):
        if (self.categories is None) or (self.categories == []): return None
        return self.categories[0]
    firstCategory = property(getFirstCategory)

    @staticmethod
    def getPlaceByOSMId(osmId):
        try:
            return Place.objects.get(osmId=osmId)
        except ObjectDoesNotExist:
            return None
    
    @staticmethod
    def toJSON(places):
        r = []
        for place in places:
            r.append({"name":place.name,"lat":place.lat,"lon":place.lon,"categories":place.categories})
        return r